#!/usr/bin/perl
use warnings;
use strict;
my $argv_num = @ARGV;
#print "***************code running**************\n";
if ($argv_num < 1) {
	die "************************************************************************
	Usage:perl ver_devel.pl filename
************************************************************************\n";
}
my $filename = shift(@ARGV);
open FILENAME, "<$filename" or die "Can't open $filename: $1\n";
open REWRITE_FILE, ">rewrite_$filename" or die "Can't create rewirte_$filename: $1\n";

while(<FILENAME>){
# 去掉前置缩进，以及尾部空格，注释都已单一#开头，重写入rewrite_$filename中
	my $line = $_;
	$line =~ s/^([\s]*)//;
	$line =~ s/#+([\w\W]+)/#$1/;
	$line =~ s/( *)\n$/\n/;
	$line =~ s/^if\(/if \(/;
	print REWRITE_FILE $line; 
}

close FILENAME;
close REWRITE_FILE;
open FILENAME, "<rewrite_$filename" or die "Can't open rewrite_$filename: $1\n";
open FORMAT_FILE, ">format_$filename" or die "Can't create format_$filename: $1\n";

my $indentlevel = 0;
my $decide_block_end = 0;
while(<FILENAME>){
	my $line = $_;
	$line =~ s/\n$//;
	my $note = '';
	my $note_cut = '';
	my $line_cut = '';
#确定是否有注释,注释=>$note,代码=>$line
	if ($line =~ s/#(.*)//){
		$note = $1;
		$note =~ s/^[\s]*//;
#		print $line;
	}else{
		$note = '';
#		print $line;
	}
#添加缩进
	my $enter_decide_block_end = 1;
	my $big_right_bracket_num = (my @big_right_bracket = ($line =~ /\}/g));
	my $big_left_bracket_num = (my @big_left_bracket = ($line =~ /\{/g));
	if ($line =~ /\{|\}/){
		if ($big_right_bracket_num == $big_left_bracket_num){
			if ($indentlevel){
				$line = (' 'x(4*$indentlevel)).$line;
				if ($note){
					$note = (' 'x(4*$indentlevel)).'#'.$note;
				}
			}
		}elsif ($big_right_bracket_num < $big_left_bracket_num){
			$indentlevel += 1;
			if (($indentlevel - 1)){
				$line = (' 'x(4*($indentlevel-1))).$line;
				if ($note){
					$note = (' 'x(4*($indentlevel-1))).'#'.$note;
				}
			}
		}else{
			$indentlevel -= 1;
			if ($indentlevel){
				$line = (' 'x(4*$indentlevel)).$line;
				if ($note){
					$note = (' 'x(4*$indentlevel)).'#'.$note;
				}
			}
			$decide_block_end = 1;
			$enter_decide_block_end = 0;
		}
	}else{
		if ($line){
			$line = (' 'x(4*$indentlevel)).$line;
		}
		if ($note){
			 $note = (' 'x(4*$indentlevel)).'#'.$note;
		}
	}
#if语句块结束换行
	if ($decide_block_end and $enter_decide_block_end){
		if ($line =~ /elsif/){
			$decide_block_end = 0;
		}else{
			print FORMAT_FILE "\n";
			$decide_block_end =0;
		}
	}
#处理过长的行
#注释行
	if (length($note) > 100){
		$note =~ s/^([\s]*)//;
		my $note_blank = $1;
		my $note_num = 99 - length($note_blank);
		my $note_count = 0;
		while(length($note) > $note_num){
			$note_count += 1;
			if ($note_count > 20){
				print "loop too many\n";
				last;
			}
			$note =~ s/^(.{$note_num})[\s]*//;
			$note_cut = $1;
			#在这里运用函数fix_length_demand函数,按照要求,确定$note,$note_cut的内容
			($note,$note_cut) = &fix_length_demand($note,$note_cut);
			print FORMAT_FILE ($note_blank."#".$note_cut."\n");
		}
		print FORMAT_FILE ($note_blank."#".$note."\n");
	}else{
		print FORMAT_FILE $note."\n" if ($note);
	}
#代码行
	if(length($line) > 100){
		$line =~ s/^([\s]*)//;
		my $line_blank = $1;
		my $line_num = 100 - length($line_blank);
		my $line_count = 0;
		while(length($line) > $line_num){
			$line_count += 1;
			if ($line_count > 20){
				print "loop too many\n";
				last;
			}
			$line =~ s/^(.{$line_num})[\s]*//;
			$line_cut = $1;
			#在这里运用函数fix_length_demand函数,按照要求,确定$note,$note_cut的内容
			($line,$line_cut) = &fix_length_demand($line,$line_cut);
			print FORMAT_FILE ($line_blank.$line_cut."\n");
		}
		print FORMAT_FILE ($line_blank.$line."\n");
	}else{
		print FORMAT_FILE $line."\n" if ($line);
	}
}
print FORMAT_FILE "\n";


=pod
	$txt和$txt_cut都没有前置空格
=cut
sub fix_length_demand
{
	my($txt,$txt_cut) = @_;
#首行逗号,首行句号,首行比较符放到上一行行尾
	if ($txt =~ s/^([,|.|=|<|>]+)( *)?//){
		my $punc = $1;
		$txt_cut .= $punc;
	}elsif ($txt =~ /^\w/){
		if ($txt_cut =~ /\$(\w*)$/){
			my $plus = $1;
			$txt_cut =~ s/( *)?\$$plus$//;
			$txt = "\$$plus".$txt;
		}
	}
#双引号不能换行
	my $double_quot_mark_num = (my @double_quotation_marks = ($txt_cut =~ /"/g));
	if ($double_quot_mark_num%2 == 1){
		if ($double_quot_mark_num > 2){
			$txt_cut =~ s/(?: *)?"([^"]+)$//;
			my $print_cont = $1;
			$txt ='"'. $print_cont.$txt;
		}else{
			$txt = '"'.$txt;
			$txt_cut .= '",';
		}
	}
#判断语句以&&分割
	if ($txt_cut =~ /^([\w\W]*)&&( *)?([^&{2}]+)$/){
		my $pre_and = $1;
		my $post_and = $3;
		if (length($pre_and) > length($post_and) and $post_and){
			$txt = $post_and.$txt;
			$txt_cut =~ s/&&( *)?[^&{2}]*$/&&/;
		}
	}
#对齐输出语句行
	if ($txt_cut =~ /^(if \(+)/){
		my $plus = length($1);
		$txt = (' 'x$plus).$txt;
	}elsif($txt_cut =~ /^([^"\$]*)["|\$]/){
		my $plus = length($1);
		$txt = (' 'x$plus).$txt;
	}elsif($txt_cut =~ /^([ ]*)/){
		my $plus = length($1);
		$txt = (' 'x$plus).$txt;
	}
	$txt_cut =~ s/ *?$//;
	return $txt,$txt_cut;
}

